package com.gdssoft.pdaweb.service.water.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.gdssoft.core.Constants;
import com.gdssoft.core.service.impl.BaseServiceImpl;
import com.gdssoft.core.util.ContextUtil;
import com.gdssoft.core.util.DateUtil;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.framework.service.system.AppUserService;
import com.gdssoft.pdaweb.dao.PdaPlanDetailDao;
import com.gdssoft.pdaweb.dao.water.WaterMeterMonthPlanDao;
import com.gdssoft.pdaweb.dao.water.WaterMeterMonthPlanReadDao;
import com.gdssoft.pdaweb.dao.water.WaterMeterPlanDao;
import com.gdssoft.pdaweb.model.PdaPlanDetail;
import com.gdssoft.pdaweb.model.water.PdaWaterMeter;
import com.gdssoft.pdaweb.model.water.WaterMeterMonthPlan;
import com.gdssoft.pdaweb.model.water.WaterMeterMonthPlanRead;
import com.gdssoft.pdaweb.model.water.WaterMeterPlan;
import com.gdssoft.pdaweb.service.PdaPlanDetailService;
import com.gdssoft.pdaweb.service.water.PdaWaterListService;
import com.gdssoft.pdaweb.service.water.PdaWaterMeterService;
import com.gdssoft.pdaweb.service.water.WaterMeterPlanService;

public class WaterMeterPlanServiceImpl extends BaseServiceImpl<WaterMeterPlan>
		implements WaterMeterPlanService {
	private WaterMeterPlanDao dao;
	@Resource
	private PdaPlanDetailService pdaPlanDetailService;
	@Resource
	private PdaWaterMeterService pdaWaterMeterService;
	@Resource
	private PdaWaterListService pdaWaterListService;
	@Resource
	private AppUserService appUserService;
	@Resource
	private PdaPlanDetailDao pdaPlanDetailDao;
	@Resource
	private WaterMeterMonthPlanDao waterMeterMonthPlanDao;
	@Resource
	private WaterMeterMonthPlanReadDao waterMeterMonthPlanReadDao;

	public WaterMeterPlanServiceImpl(WaterMeterPlanDao dao) {
		super(dao);
		this.dao = dao;
	}

	@Override
	public List<String> getByPlanId(Long planId) {
		List<PdaPlanDetail> list = pdaPlanDetailService.getByPlanId(planId,
				PdaPlanDetail.WATER);
		List<String> amids = new ArrayList<String>();

		for (PdaPlanDetail ppd : list) {
			amids.add(ppd.getMeterId());
		}
		return amids;
	}

	@Override
	public WaterMeterPlan save(WaterMeterPlan waterMeterPlan, String[] ids,String[] removeds) throws Exception {
		Date now = new Date();
		String un = ContextUtil.getCurrentUser().getUsername();
		if (waterMeterPlan.getPlanId()!=null) {
			List<PdaPlanDetail> ls = pdaPlanDetailDao.getByPlanId(
					waterMeterPlan.getPlanId(), PdaPlanDetail.WATER);
			for (PdaPlanDetail pdaPlanDetail : ls) {		//clear old plan detail
				pdaPlanDetailService.remove(pdaPlanDetail.getId());
			}

		}
		try {
			waterMeterPlan = super.save(waterMeterPlan);	//save plan 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for(String amid : removeds){		//mark as unplanned
			PdaWaterMeter wm = pdaWaterMeterService.getByMeterId(amid);
			wm.setIsPlanned(Constants.UNPLANNED);
			pdaWaterMeterService.save(wm);	
		}
		
		for (String amid : ids) {	//add plan detail
			PdaPlanDetail pdaPlanDetail = new PdaPlanDetail();
			pdaPlanDetail.setPlanId(waterMeterPlan.getPlanId());
			pdaPlanDetail.setMeterId(amid);
			pdaPlanDetail.setType(PdaPlanDetail.WATER);
			pdaPlanDetail.setCreateddt(now);
			pdaPlanDetail.setCreatedby(un);
			pdaPlanDetailDao.save(pdaPlanDetail);	//保存Plan Detail
			
			PdaWaterMeter wm = pdaWaterMeterService.getByMeterId(amid);
			wm.setIsPlanned(Constants.PLANNED);//标为已计划
			pdaWaterMeterService.save(wm);
		}

		return waterMeterPlan;
	}

	@Override
	public boolean execute(long planId, Date beginTime, Date endTime,String username) {
		boolean result = true;
		Date now = new Date();
		String thisMonth = new SimpleDateFormat("yyyy-MM").format(new Date());
		String lastMonth = DateUtil.getLastMonthStr(thisMonth);
		String un = ContextUtil.getCurrentUser().getUsername();

		// 更新状态
		WaterMeterPlan waterMeterPlan = dao.get(planId);
		waterMeterPlan.setPlanStatus((long) 1);
		waterMeterPlan.setLastEditBy(un);
		waterMeterPlan.setLastEditDt(new Date());
		//String executor = waterMeterPlan.getExecutor();
		dao.save(waterMeterPlan);

		// 写入月计划表
		WaterMeterMonthPlan waterMeterMonthPlan = new WaterMeterMonthPlan();
		waterMeterMonthPlan.setBeginTime(beginTime);
		waterMeterMonthPlan.setEndTime(endTime);
		waterMeterMonthPlan.setMonth(thisMonth);
		waterMeterMonthPlan.setPlanId(planId);
		waterMeterMonthPlan.setPlanStatus((long) 1);
		waterMeterMonthPlan.setCreatedby(un);
		waterMeterMonthPlan.setCreateddt(now);
		waterMeterMonthPlanDao.save(waterMeterMonthPlan);
		waterMeterMonthPlan = null;
		waterMeterPlan = null;

		PdaWaterMeter pdaWaterMeter = null;
		List<PdaPlanDetail> list = pdaPlanDetailService.getByPlanId(planId,
				PdaPlanDetail.WATER);
		for (PdaPlanDetail pdaPlanDetail : list) {
			WaterMeterMonthPlanRead waterMeterMonthPlanRead = new WaterMeterMonthPlanRead();
			waterMeterMonthPlanRead.setPlanID(planId);
			waterMeterMonthPlanRead.setExecutor(username);
			waterMeterMonthPlanRead.setCreatedby(un);
			waterMeterMonthPlanRead.setCreateddt(now);
			waterMeterMonthPlanRead.setMonth(thisMonth);
			pdaWaterMeter = pdaWaterMeterService.getByMeterId(pdaPlanDetail
					.getMeterId());
			waterMeterMonthPlanRead
					.setAparmentID(pdaWaterMeter.getAparmentId());
			waterMeterMonthPlanRead
					.setBuildingID(pdaWaterMeter.getBuildingId());
			waterMeterMonthPlanRead
					.setDistrictID(pdaWaterMeter.getDistrictId());
			long lastDegree = pdaWaterListService.getDegreeByTime(
					pdaPlanDetail.getMeterId(), lastMonth);
			waterMeterMonthPlanRead.setLastDegree(lastDegree);
			waterMeterMonthPlanRead.setDegree(0L); // default degree=0
			waterMeterMonthPlanRead.setMeterID(pdaWaterMeter.getAmId());
			waterMeterMonthPlanReadDao.save(waterMeterMonthPlanRead);
		}

		return result;
	}

	@Override
	public List<AppUser> getExecutor() {
		return appUserService.findByRoleCode("ROLE_WATER_READING");
	}

	@Override
	public List<WaterMeterMonthPlanRead> getPlanTask(long planId, String month) {
		List<WaterMeterMonthPlanRead> retLs = new ArrayList<WaterMeterMonthPlanRead>();
		List<PdaPlanDetail> ls = pdaPlanDetailService.getByPlanId(planId,PdaPlanDetail.WATER);

		for (PdaPlanDetail pdaPlanDetail : ls) {
			String meterId = pdaPlanDetail.getMeterId();
			List<WaterMeterMonthPlanRead> waterMeterMonthPlanReadList = waterMeterMonthPlanReadDao.getPlanTask(meterId, month,planId);

			if (waterMeterMonthPlanReadList.size() > 0) {
				retLs.add(waterMeterMonthPlanReadList.get(0));
			}

		}
		return retLs;
	}

}
